import 'package:flutter/material.dart';
import 'package:flutter_vant_kit/widgets/addressEdit.dart';
import 'package:flutter_vant_kit/widgets/addressList.dart';
import 'package:shoes_shop/api/api.dart';
import 'package:shoes_shop/core/utils.dart';

class EditAddressPage extends StatelessWidget {
  final AddressInfo info;
  const EditAddressPage({Key key, this.info}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        // backgroundColor: AppColors.purplishBlue,
        title: Text(info == null ? '添加地址' : '编辑地址'),
      ),
      body: ListView(
        children: [
          AddressEdit(
              showDelete: info != null,
              showSetDefault: true,
              addressInfo: {
                "name": info.name, //张三
                "tel": info.tel, //18345234123
                "province": info.province,
                "city": info.city,
                "county": info.county,
                "provinceId": info.provinceId,
                "cityId": info.cityId,
                "countyId": info.countyId,
                "addressDetail": info.addressDetail, //明珠花园
                "postalCode": info.postalCode, //515000
                "isDefault": info.isDefault
              },
              onSave: (data) async {
                print(data.toString());
                if (data['name'] == '') {
                  showToast('请输入姓名');
                  return;
                }
                if (data['tel'].length != 11) {
                  showToast('请输入正确的手机号');
                  return;
                }
                if (data['province'] == '') {
                  showToast('请选择地区');
                  return;
                }
                if (data['addressDetail'] == '') {
                  showToast('请输入详细地址');
                  return;
                }
                if (data['postalCode'].length != 6) {
                  showToast('请输入正确的邮政编码，如不知道请填写 000000');
                  return;
                }
                Map res;
                if (info == null) {
                  // 新增地址
                  res = await addAddress(data);
                } else {
                  // 修改地址
                  res = await editAddress(info.id, data);
                }
                showToast(res['message']);
                if (res['code'] == 200) {
                  // _getAddressList(context);
                  Navigator.pop(context);
                }
              },
              onDelete: (data) async {
                print('删除${info.id}');
                Map res = await deleteAddress(info.id);
                showToast(res['message']);
                if (res['code'] == 200) {
                  Navigator.pop(context);
                }
              }),
        ],
      ),
    );
  }
}
